﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace SixApp.Core
{
    [Serializable]
    public class Entity 
    {
        private static PropertyInfo[] mProperties;

        public virtual void Load(DataRow row)
        {
            if (mProperties == null)
            {
                mProperties = GetProperties();
            }

            foreach (PropertyInfo p in mProperties)
            {
                if (!p.IsSpecialName && p.CanRead && p.CanWrite)
                {
                    bool found = false;
                    foreach (DataColumn c in row.Table.Columns)
                    {
                        if (c.ColumnName.Equals(p.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found && !(row[p.Name] is DBNull))
                    {
                        p.SetValue(this, row[p.Name], null);
                    }
                }
            }
        }

        private PropertyInfo[] GetProperties()
        {
            return this.GetType().GetProperties();
        }

    }
}
